<?php
defined('APP') or die('Hacking attemp!');

class Model_User extends Core_Model
{
	public function __construct(){
		parent::__construct();
	}
	public function getCheckUser($u){
		if(!empty($u)){
			$query = 'SELECT Username FROM users WHERE Username = "'.$u.'"';
			$result = Core_Model::excuteQuery($query);
			if($result->num_rows==1){
				//da co user nay trong co so du lieu
				return true;
			}else{
				return false;
			}
			Core_Model::freeResult($result);
		}
		return false;
	}
	public function getCheckEmail($email){
		if(!empty($email)){
			$query = 'SELECT Email FROM users WHERE Email = "'.$email.'"';
			$result = Core_Model::excuteQuery($query);
			if($result->num_rows==1){
				//Email nay trong co so du lieu
				return true;
			}else{
				return false;
			}
			Core_Model::freeResult($result);
		}
		return false;
	}
    public function getLogin($u, $p){
		if(!empty($u) && !empty($p)){//neu 2 thong tin nay rong thi false
			$query = 'SELECT * FROM users WHERE Username = "' .$u. '" AND Password = "' .sha1($p). '"';
			$result = Core_Model::excuteQuery($query);
			if($result->num_rows==1){
				$row = $result->fetch_array();
				if($row['Permissions']==1)
					return 1;
                return 2;
			}else{
                return 0;
			}
			Core_Model::freeResult($result);
		}
		return 0;
    }

	public function insertUser($username, $password, $fullname, $email, $phone, $address){
		if(!empty($username) && !empty($password) && !empty($fullname) && !empty($address) && !empty($phone)){
			$query = 'INSERT INTO users(Username, Password, Fullname, Email, Phone, Address) VALUES("'.$username.'","'.sha1($password).'","'.$fullname.'","'.$email.'","'.$phone.'","'.$address.'")';
			$result = Core_Model::excuteQuery($query);
			if($result==1){
				return true;
			}else{
				return false;
			}
			Core_Model::freeResult($result);
		}
		return false;
	}
	public function updateUser($username, $password, $fullname, $phone, $address){
		if(!empty($password) && !empty($fullname) && !empty($address) && !empty($phone)){
			$query = 'UPDATE users SET Password = "'.sha1($password).'", Fullname = "'.$fullname.'", Phone = "'.$phone.'", Address = "'.$address.'" WHERE Username = "'.$username.'"';
			$result = Core_Model::excuteQuery($query);
			if($result==1){
				return true;
			}else{
				return false;
			}
			Core_Model::freeResult($result);
		}
		return false;
	}

}